package com.lemon.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.*;
import com.lemon.domain.dto.JianHuoDto;
import com.lemon.mapper.OutboundOrderMapper;
import com.lemon.mapper.OutboundOrderPackageMapper;
import com.lemon.mapper.SkuMapper;
import com.lemon.utils.UserThreadLocal;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.mapper.OutboundOrderDetailsMapper;
import com.lemon.service.OutboundOrderDetailsService;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class OutboundOrderDetailsServiceImpl extends ServiceImpl<OutboundOrderDetailsMapper, OutboundOrderDetails> implements OutboundOrderDetailsService{
    private final OutboundOrderMapper outboundOrderMapper;
    private final OutboundOrderDetailsMapper outboundOrderDetailsMapper;
    private final OutboundOrderPackageMapper outboundOrderPackageMapper;
    private final SkuMapper skuMapper;

    @Override
    @Transactional
    public void jianhuo(Long outboundOrderDetailsId, Integer status) {
        OutboundOrderDetails outboundOrderDetails = outboundOrderDetailsMapper.selectById(outboundOrderDetailsId);
        outboundOrderDetails.setPickStatus(status);
        outboundOrderDetails.setPickEmpId(UserThreadLocal.get().getId());
        outboundOrderDetails.setPickTime(LocalDateTime.now());
        outboundOrderDetailsMapper.updateById(outboundOrderDetails);
        OutboundOrder outboundOrder = outboundOrderMapper.selectById(outboundOrderDetails.getOutboundOrderId());
        outboundOrder.setStatus(4);
        outboundOrderMapper.updateById(outboundOrder);
        OutboundOrderPackage aPackage = new OutboundOrderPackage();
        aPackage.setPackageCode(outboundOrder.getInvoiceCode());
        aPackage.setOutboundOrderDetailId(outboundOrderDetailsId);
        aPackage.setSkuId(outboundOrderDetails.getSkuId());
        aPackage.setStatus(1);
        aPackage.setPackageCount(0);
        aPackage.setCreateTime(LocalDateTime.now());
        aPackage.setIsDel(1);
        outboundOrderPackageMapper.insert(aPackage);
    }

    @Override
    public PageResult getPage(Integer page, Integer pageSize, String invoiceCode, Integer status) {
        // 先查询出符合条件的 outboundOrderIds
        List<Long> orderIds = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(invoiceCode)) {
            List<OutboundOrder> orders = outboundOrderMapper.selectList(Wrappers.<OutboundOrder>lambdaQuery()
                                                                                .like(OutboundOrder::getInvoiceCode, invoiceCode));
            orderIds = orders.stream()
                               .map(OutboundOrder::getId)
                               .toList();
            // 如果根据发票编号查询不到订单，则直接返回空结果
            if (CollUtil.isEmpty(orderIds)) {
                return new PageResult(0L, new ArrayList<>());
            }
        }

        // 开启分页
        PageHelper.startPage(page, pageSize);

        // 执行查询，PageHelper会自动对这个查询进行分页
        List<OutboundOrderDetails> outboundOrderDetails = outboundOrderDetailsMapper.selectList(
                Wrappers.<OutboundOrderDetails>lambdaQuery()
                        .in(CollUtil.isNotEmpty(orderIds), OutboundOrderDetails::getOutboundOrderId, orderIds)
                        .eq(status != null, OutboundOrderDetails::getPickStatus, status)
        );

        // 转换为 JianHuoDto
        List<JianHuoDto> jianHuoDtos = new ArrayList<>();
        outboundOrderDetails.forEach(orderDetails -> {
            JianHuoDto jianHuoDto = new JianHuoDto();
            jianHuoDto.setId(orderDetails.getId());
            OutboundOrder outboundOrder = outboundOrderMapper.selectOne(Wrappers.<OutboundOrder>lambdaQuery()
                                                                                .eq(OutboundOrder::getId, orderDetails.getOutboundOrderId()));
            jianHuoDto.setInvoiceCode(outboundOrder.getInvoiceCode());
            jianHuoDto.setCode(orderDetails.getCode());
            Sku sku = skuMapper.selectById(orderDetails.getSkuId());
            jianHuoDto.setTitle(sku.getTitle());
            jianHuoDto.setPreCount(orderDetails.getPreCount());
            jianHuoDto.setProductWeight(sku.getProductWeight());
            jianHuoDto.setProductVolume(sku.getProductVolume());
            jianHuoDto.setCustomerId(outboundOrder.getCustomerId());
            jianHuoDto.setCreateTime(outboundOrder.getCreateTime());
            jianHuoDto.setPickEmpId(orderDetails.getPickEmpId());
            jianHuoDtos.add(jianHuoDto);
        });

        // 获取分页信息
        Page<OutboundOrderDetails> pageResult = (Page<OutboundOrderDetails>) outboundOrderDetails;
        return new PageResult(pageResult.getTotal(), jianHuoDtos);
    }


    @Override
    public List<OutboundOrderDetails> getList(Long outboundOrderId) {
        List<OutboundOrderDetails> outboundOrderDetails = outboundOrderDetailsMapper.selectList(Wrappers.<OutboundOrderDetails>lambdaQuery().eq(OutboundOrderDetails::getOutboundOrderId, outboundOrderId).eq(OutboundOrderDetails::getIsDel, 1));
        outboundOrderDetails.forEach(details -> details.setSkuName(skuMapper.selectById(details.getSkuId()).getTitle()));
        return outboundOrderDetails;
    }

}
